//
//  YZFriendBaseCell.m
//  VET
//
//  Created by lixiaoyi on 2018/7/11.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZFriendBaseCell.h"
#import "UIImageView+WebCache.h"

@implementation YZFriendBaseCell

#pragma mark - init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createViews];
        [self layouViews];
    }
    
    return self;
}

- (void)createViews{
    [self.contentView addSubview:self.iconImageView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.infoLabel];
    [self.contentView addSubview:self.friendStateBtn];
    [self.contentView addSubview:self.ignoreBtn];
}

#pragma mark - lazy load
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView  = [UIImageView new];
        [_iconImageView setFrame:CGRectMake(10, 10, 50, 50)];
    }
    
    return _iconImageView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.font = [UIFont systemFontOfSize:17];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
    }
    
    return _nameLabel;
}

- (UILabel *)infoLabel {
    if (!_infoLabel) {
        _infoLabel = [UILabel new];
        _infoLabel.textColor = [UIColor lightGrayColor];
        _infoLabel.font = [UIFont systemFontOfSize:15];
        _infoLabel.textAlignment = NSTextAlignmentLeft;
        
    }
    
    return _infoLabel;
}

- (UIButton *)friendStateBtn {
    if (!_friendStateBtn) {
        _friendStateBtn = [UIButton new];
    }
    
    return _friendStateBtn;
}

- (UIButton *)ignoreBtn {
    if (!_ignoreBtn) {
        _ignoreBtn = [UIButton new];
    }
    
    return _ignoreBtn;
}

#pragma mark - layout
- (void)refreshFriendState {
    if (self.model.friendState == NewFriendStateAgreed) {
        
        [_friendStateBtn setTitle:@"已同意" forState:UIControlStateNormal];
        [_friendStateBtn setImage:[UIImage imageNamed:@"agree"] forState:UIControlStateNormal];
        [_friendStateBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_friendStateBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_friendStateBtn setBackgroundColor:[UIColor clearColor]];
    } else if (self.model.friendState == NewFriendStateBeAgree) {
        // 忽略
        [_ignoreBtn setTitle:@"忽略" forState:UIControlStateNormal];
        [_ignoreBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_ignoreBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        _ignoreBtn.layer.cornerRadius = 10.0f;
        _ignoreBtn.layer.masksToBounds = YES;
        _ignoreBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _ignoreBtn.layer.borderWidth = 1.0f;
        
        [_ignoreBtn addTarget:self action:@selector(ignoreFriend:) forControlEvents:UIControlEventTouchUpInside];
         
        
        // 同意
        [_friendStateBtn setTitle:@"同意" forState:UIControlStateNormal];
        [_friendStateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_friendStateBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        _friendStateBtn.backgroundColor = kColorWithHex(0x32BE60);
        _friendStateBtn.layer.cornerRadius = 10.0f;
        _friendStateBtn.layer.masksToBounds = YES;
        [_friendStateBtn addTarget:self action:@selector(addFriend:) forControlEvents:UIControlEventTouchUpInside];
    } else if (self.model.friendState == NewFriendStateIgnored) {
        [_friendStateBtn setTitle:@"已忽略" forState:UIControlStateNormal];
        [_friendStateBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_friendStateBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        _friendStateBtn.backgroundColor = [UIColor clearColor];
    }
}

- (void)layouViews{
    [_friendStateBtn setFrame:CGRectMake(0, 0, 60, 30)];
    [_ignoreBtn setFrame:CGRectMake(0, 0, 60, 30)];
    
    UIView *superView = self.contentView;
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(superView.mas_left).with.offset(10);
        make.top.mas_equalTo(superView.mas_top).with.offset(10);
        make.right.mas_equalTo(self.nameLabel.mas_left).with.offset(-10);
        make.bottom.mas_equalTo(superView.mas_bottom).with.offset(-10);
        make.width.height.mas_equalTo(50);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImageView.mas_right).with.offset(10);
        make.top.mas_equalTo(superView.mas_top).with.offset(10);
        make.right.mas_equalTo(self.ignoreBtn.mas_left).with.offset(0);
        make.bottom.mas_equalTo(self.infoLabel.mas_top).with.offset(-5);
        make.width.mas_equalTo(120);
    }];
    
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_left);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).with.offset(5);
        make.bottom.mas_equalTo(superView.mas_bottom).with.offset(-10);
        make.right.mas_equalTo(self.nameLabel.mas_right);
    }];
    
    [self.ignoreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_right).with.offset(100);
        make.right.mas_equalTo(self.friendStateBtn.mas_left).with.offset(-10);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(superView);
    }];
    
    
    [self.friendStateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.ignoreBtn.mas_right).with.offset(10);
        make.right.mas_equalTo(superView.mas_right).with.offset(-10);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(superView);
    }];
}

#pragma mark - setter
- (void)setModel:(YZFriendModel *)model {
    _model = model;
    NSURL *iconUrl = [NSURL URLWithString: model.iconName];
    [self.iconImageView sd_setImageWithURL:iconUrl placeholderImage:nil];
    [self.nameLabel setText:model.name ? : @""];
    [self.infoLabel setText:model.info ? : @""];
    
    if (model.friendState == NewFriendStateNone) {
        [self.friendStateBtn setHidden:YES];
    } else if (model.friendState == NewFriendStateAgreed | model.friendState == NewFriendStateIgnored) {
        [self.ignoreBtn setHidden:YES];
    }
    else {
        [self.friendStateBtn setHidden:NO];
        [self.ignoreBtn setHidden:NO];
    }
    
    [self refreshFriendState];
}

#pragma mark - addFriend
- (void)addFriend:(UIButton *)sender {
    NSLog(@"点了添加");
    if (self.cellBtnClickBlock) {
        self.cellBtnClickBlock(YES);
    }
}

- (void)ignoreFriend:(UIButton *)sender {
    NSLog(@"点了忽略");
    if (self.cellBtnClickBlock) {
        self.cellBtnClickBlock(NO);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


@end
